package com.itheima.marking_system.utils.baidu;

import java.util.*;

import org.json.JSONArray;
import org.json.JSONObject;
import com.baidu.aip.ocr.AipOcr;
public class BaiduUtils {
    //设置APPID/AK/SK
    public static final String APP_ID = "106290122";
    public static final String API_KEY = "1oWom4Pq1v3QfGGhMlFEH0pJ";
    public static final String SECRET_KEY = "8wjLHOXgNPdRxkjCcOTUOIRS9r5pyu6k";



    public static String getOcrResult(byte[] imgBytes) {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
        // 调用接口
        JSONObject res = client.basicGeneral(imgBytes, new HashMap<String, String>());
        // 输出结果
        JSONArray wordsResult = res.getJSONArray("words_result");
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < wordsResult.length(); i++) {
            JSONObject word = wordsResult.getJSONObject(i);
            String words = word.getString("words");
            if (i == 0) {
                result.append(words);
            } else {
                result.append(" ").append(words);
            }
        }
        return result.toString();
    }

}